package com.example.gitstudy.easypoi.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.example.gitstudy.easypoi.entity.Member;
import com.example.gitstudy.easypoi.entity.Order;
import com.example.gitstudy.easypoi.entity.Product;
import com.example.gitstudy.easypoi.entity.ZscqNoticeKey;
import com.example.gitstudy.easypoi.service.ZscqNoticeKeyService;
import com.example.gitstudy.handler.MemberExcelDataHandler;
import com.example.gitstudy.util.LocalJsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author hwj
 * @date 2022/12/9 14:36
 * @描述；easyppoi测试
 */
@RestController
@RequestMapping("easyPoi")
@Api(tags = "easypoi导入导出excel")
public class EasyPoiController{
    @Autowired
    private ZscqNoticeKeyService zscqNoticeKeyService;

    @ApiOperation("导出")
    @GetMapping("/exportListInfo")
    public  void  exportList(ModelMap map, HttpServletRequest request, HttpServletResponse response){
        List<ZscqNoticeKey> zscqNoticeKeys = zscqNoticeKeyService.list();
        ExportParams params = new ExportParams("通知列表", "通知列表", ExcelType.XSSF);
        map.put(NormalExcelConstants.DATA_LIST, zscqNoticeKeys); //导出的数据列表
        map.put(NormalExcelConstants.CLASS, ZscqNoticeKey.class);//对应的实体类
        map.put(NormalExcelConstants.PARAMS, params);//模板相关参数
        map.put(NormalExcelConstants.FILE_NAME, "通知列表");//文件名称
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);

    }

    @ApiOperation("导入")
    @PostMapping("/importExcel")
    public List<ZscqNoticeKey> importExcel(@RequestPart("file") MultipartFile file){

        ImportParams params = new ImportParams();
        params.setTitleRows(1); //titleRows：表格标题行数,默认0
        params.setHeadRows(1);//headRows：表头行数,默认1
        try {
            List<ZscqNoticeKey> list = ExcelImportUtil.importExcel(
                    file.getInputStream(),
                    ZscqNoticeKey.class, params);
            zscqNoticeKeyService.saveBatch(list);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @ApiOperation(value = "导出会员列表Excel")
    @RequestMapping(value = "/exportMemberList", method = RequestMethod.GET)
    public void exportMemberList(ModelMap map,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {
        List<Member> memberList = LocalJsonUtil.getListFromJson("json/members.json", Member.class);
        ExportParams params = new ExportParams("会员列表", "会员列表", ExcelType.XSSF);
        //对导出结果进行自定义处理
        MemberExcelDataHandler handler = new MemberExcelDataHandler();
        handler.setNeedHandlerFields(new String[]{"昵称","用户名"});
        params.setDataHandler(handler);
        map.put(NormalExcelConstants.DATA_LIST, memberList);
        map.put(NormalExcelConstants.CLASS, Member.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "memberList");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    @ApiOperation(value = "导出订单列表Excel")
    @RequestMapping(value = "/exportOrderList", method = RequestMethod.GET)
    public void exportOrderList(ModelMap map,
                                HttpServletRequest request,
                                HttpServletResponse response) {
        List<Order> orderList = getOrderList();
        ExportParams params = new ExportParams("订单列表", "订单列表", ExcelType.XSSF);
        //导出时排除一些字段
        params.setExclusions(new String[]{"ID", "出生日期", "性别"});
        map.put(NormalExcelConstants.DATA_LIST, orderList);
        map.put(NormalExcelConstants.CLASS, Order.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "orderList");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

    private List<Order> getOrderList() {
        List<Order> orderList = LocalJsonUtil.getListFromJson("json/orders.json", Order.class);
        List<Product> productList = LocalJsonUtil.getListFromJson("json/products.json", Product.class);
        List<Member> memberList = LocalJsonUtil.getListFromJson("json/members.json", Member.class);
        for (int i = 0; i < orderList.size(); i++) {
            Order order = orderList.get(i);
            order.setMember(memberList.get(i));
            order.setProductList(productList);
        }
        return orderList;
    }


}
